package com.xiaomili.halfsugar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.ListDetailBean;

/**
 * Created by WangQi on 2016/7/23.
 */
public class ListDetailRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ListDetailBean.DataBean mData;
    private static final int NORMAL_TYPE = 0;
    private static final int ORIGINAL_TYPE = 1;

    public ListDetailRecyclerViewAdapter(Context context, ListDetailBean.DataBean data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ORIGINAL_TYPE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.listdetail_recyclerview_original_item, parent, false);
            return new OriginalViewHolder(view);
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.listdetail_recyclerview_normal_item, parent, false);
        return new NormalViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i("test", "type: " + getItemViewType(position));
        if (getItemViewType(position) == NORMAL_TYPE) {
            NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
            normalViewHolder.tv_title.setText(mData.getProduct_list().get(position).getTitle());
            normalViewHolder.tv_desc.setText(mData.getProduct_list().get(position).getDesc());
            int imgSize = mData.getProduct_list().get(position).getPics().size();
            if (normalViewHolder.ll_img != null) {
                normalViewHolder.ll_img.removeAllViews();
                for (int i = 0; i < imgSize; i++) {
                    ImageView iv = (ImageView) LayoutInflater.from(mContext).inflate(R.layout.image, null);
                    Picasso.with(mContext).load(mData.getProduct_list().get(position).getPics().get(i).getUrl()).into(iv);
                    normalViewHolder.ll_img.addView(iv);
                }
            }
            normalViewHolder.tv_product_name.setText(mData.getProduct_list().get(position).getTitle());
            Picasso.with(mContext).load(mData.getProduct_list().get(position).getPic()).into(normalViewHolder.iv_product_img);
            normalViewHolder.tv_product_price.setText(mData.getProduct_list().get(position).getPrice());
            normalViewHolder.tv_likes.setText(mData.getProduct_list().get(position).getLikes());
        }

        if (getItemViewType(position) == ORIGINAL_TYPE) {
            OriginalViewHolder originalViewHolder = (OriginalViewHolder) holder;
            Log.i("test", "getContent_list: "+mData.getContent_list().size());
            for (int i = 0; i < mData.getContent_list().size(); i++) {
                if (mData.getContent_list().get(i).getType() == 1) {
                    LinearLayout ll = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.listdetail_recyclerview_item1, null);
                    ImageView iv = (ImageView) ll.findViewById(R.id.iv_img);
                    Picasso.with(mContext).load(mData.getContent_list().get(i).getImage_url()).into(iv);
                    originalViewHolder.ll_original.addView(ll);

                } else if (mData.getContent_list().get(i).getType() == 2) {
                    LinearLayout ll = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.listdetail_recyclerview_item2, null);
                    TextView tv = (TextView) ll.findViewById(R.id.tv_desc);
                    tv.setText(mData.getContent_list().get(i).getText_content());
                    originalViewHolder.ll_original.addView(ll);
                } else {
                    LinearLayout ll = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.listdetail_recyclerview_item5, null);
                    ImageView iv_product_img = (ImageView) ll.findViewById(R.id.iv_product_img);
                    TextView item_subtitle = (TextView) ll.findViewById(R.id.item_subtitle);
                    TextView tv_product_name = (TextView) ll.findViewById(R.id.tv_product_name);
                    TextView tv_product_price = (TextView) ll.findViewById(R.id.tv_product_price);
                    TextView tv_likes = (TextView) ll.findViewById(R.id.tv_likes);
                    Picasso.with(mContext).load(mData.getContent_list().get(i).getPic()).into(iv_product_img);
                    item_subtitle.setText(mData.getContent_list().get(i).getItem_subtitle());
                    tv_product_name.setText(mData.getContent_list().get(i).getItem_name());
                    tv_product_price.setText(mData.getContent_list().get(i).getPrice());
                    tv_likes.setText(mData.getContent_list().get(i).getLikes());
                    originalViewHolder.ll_original.addView(ll);
                }
            }
        }

    }

    @Override
    public int getItemViewType(int position) {
        String type = mData.getType();
        if (type.equals("")) {
            return NORMAL_TYPE;
        }
        return ORIGINAL_TYPE;
    }

    @Override
    public int getItemCount() {
        if (mData.getType().equals("")) {
            return mData.getProduct_list().size();
        }
        return mData.getContent_list().size();
    }

    class NormalViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title, tv_desc, tv_product_name, tv_product_price, tv_likes;

        private ImageView iv_product_img;

        private LinearLayout ll_img;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_desc = (TextView) itemView.findViewById(R.id.tv_desc);
            tv_product_name = (TextView) itemView.findViewById(R.id.tv_product_name);
            tv_product_price = (TextView) itemView.findViewById(R.id.tv_product_price);
            tv_likes = (TextView) itemView.findViewById(R.id.tv_likes);
            ll_img = (LinearLayout) itemView.findViewById(R.id.ll_img);
            iv_product_img = (ImageView) itemView.findViewById(R.id.iv_product_img);

        }
    }

    class OriginalViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_original;

        public OriginalViewHolder(View itemView) {
            super(itemView);
            ll_original = (LinearLayout) itemView.findViewById(R.id.ll_original);
        }
    }
}
